import sys


class SpiderManage(object):
    __instance = None  # 是否是第一次创建

    def __new__(cls, *args, **kwargs):
        '''
        唯一驱动器, 单实例模式
        :return: obj
        '''
        if cls.__instance is None:
            cls.__instance = object.__new__(cls)

        return cls.__instance

    def __init__(self, obj_spider, sets):
        '''
        初始化
        :param obj_spider: 启动爬虫对象导入
        :param setting: 配置文件地址导入
        '''
        self.spider = obj_spider
        # 导入用户配置文件
        sys.path.append(sets)
        import setting
        self.setting = setting.__dict__

    def start_spider(self) -> None:
        '''
        开始爬虫方法
        :return:
        '''

        spider = self.spider(self.setting)

        spider.run()

        self.end_spider()

    def end_spider(self):

        opcv = self.setting.get("PERSERVER", [])
        for op in opcv:
            op.run()

